/**
 * 合同管理
 */
Ext.namespace('Divo');
Ext.namespace('Divo.app');

Divo.app.BaseClassList = function(config) {
  /* ----------------------- private属性 ----------------------- */
  var canEdit = false, status = "list"; // 当前列表状态("list","insert","edit")
  var gridEl = 'base-grid';
  var grid, form, selModel, toolbar, typecode = "no";
  var yesPic = 'resources/img/lov_select.gif';
  var noPic = 'resources/img/lov_cancel.gif';
  var baseType = config.type;
  var cId, win, loanType, data;

  /* ----------------------- private方法 ----------------------- */
  // 创建列表
  function createGrid() {
    var queryFlds = new Array();
    var searchFlds = new Ext.util.MixedCollection();
    queryFlds.push('contractNo');
    queryFlds.push('type');
    queryFlds.push('supplier');
    queryFlds.push('enterprise');
    queryFlds.push('bank');
    queryFlds.push('logistics');
    var fldAccount = new Ext.form.TextField({
          name : 'contractNo',
          id : gridEl + '-contractNo',
          fieldLabel : '合同号',
          allowBlank : true,
          width : 100
        });
    var fldType = new Ext.form.TextField({
          name : 'type',
          id : gridEl + '-type',
          fieldLabel : '合同类别',
          allowBlank : true,
          width : 100
        });
    var fldSupplier = new Ext.form.TextField({
          name : 'supplier',
          id : gridEl + '-supplier',
          fieldLabel : '贷款企业',
          allowBlank : true,
          width : 100
        });
    var fldEnterprise = new Ext.form.TextField({
          name : 'enterprise',
          id : gridEl + '-enterprise',
          fieldLabel : '核心企业',
          allowBlank : true,
          width : 100
        });
    var fldBank = new Ext.form.TextField({
          name : 'bank',
          id : gridEl + '-bank',
          fieldLabel : '银行',
          allowBlank : true,
          width : 100
        });
    var fldLogistics = new Ext.form.TextField({
          name : 'logistics',
          id : gridEl + '-logistics',
          fieldLabel : '物流中心',
          allowBlank : true,
          width : 100
        });
    searchFlds.add('contractNo', fldAccount);
    searchFlds.add('type', fldType);
    searchFlds.add('supplier', fldSupplier);
    searchFlds.add('enterprise', fldEnterprise);
    searchFlds.add('bank', fldBank);
    searchFlds.add('logistics', fldLogistics);

    grid = new Divo.Base.GridView({
          gridId : gridEl,
          queryFlds : queryFlds,
          searchFlds : searchFlds,
          entityClassName : 'org.ssh.pm.run.entity.Contract',
          queryUrl : 'contract/query',
          deleteUrl : 'contract/delete',
          jsonId : 'id',
          recordPk : ["id"]
        });

    selModel = grid.getSelectionModel();
    selModel.on('rowselect', onRowSelect);

  }

  // 选择某行
  function onRowSelect(selModel, rowIndex, record) {

  }

  // 刷新列表
  function reloadRecords() {
    grid.getStore().reload();
  }

  // 创建新的记录，初始化表单
  function createNewRecord() {
    form.loadRecord(new form.dataRecordMeta({}));

    win = new Ext.Window({
          title : '新增信贷关系信息',
          iconCls : 'icon-win',
          autoHeight : true,
          width : 620,
          layout : 'form',
          plain : false,
          buttonAlign : 'right',
          closable : false,
          modal : true,
          items : [form]
        });
    var yesBtn = win.addButton({
          text : '确定'
        }, commitForm, this);
    var cancelBtn = win.addButton({
          text : '关闭'
        }, onWinHide, this);

    win.show();
  }
  function onWinHide() {
    win.hide();
  }

  // 创建form表单
  function createForm() {
    form = new Divo.app.ContractForm({
          layout : 'fit',
          region : 'east',
          border : true,
          split : true,
          width : 620,
          minSize : 300,
          maxSize : 400,
          submitUrl : 'contract/save'
        });

    form.on('commitFormSuccess', afterCommitFormSuccess);
  }

  // 新建或修改表单成功提交后,重新加载其父节点
  function afterCommitFormSuccess(operation, recordData) {
    grid.getStore().reload();
  }

  // 创建layout
  function createLayout() {
    toolbar = new Ext.Toolbar({
          items : [{
                xtype : "button",
                cls : "x-btn-icon",
                icon : "resources/img/g_rec_src.png",
                tooltip : "过滤数据",
                id : "tlb_FILTER",
                handler : toggleFilterBar
              }, '-', {
                icon : "resources/img/g_rec_new.png",
                text : "新增",
                id : "tlb_NEW",
                handler : createNewRecord
              }, '-', {
                icon : "resources/img/g_rec_upd.png",
                text : "编辑",
                id : "tlb_EDIT",
                handler : toggleEditMode
              }, '-', {
                icon : "resources/img/g_rec_del.png",
                text : "删除",
                id : "tlb_DELETE",
                handler : deleteRecord
              }, '-', {
                icon : "resources/img/g_rec_del.png",
                text : "作废",
                id : "tlb_CANCEL",
                handler : cancelRecord
              }, '-', {
                icon : "resources/img/g_rec_refresh.gif",
                text : "刷新",
                id : "tlb_REFRESH",
                handler : reloadRecords
              }]
        });

    var viewport = new Ext.Viewport({
          layout : 'border',
          items : [{
                layout : 'fit',
                region : 'north',
                border : false,
                items : [toolbar],
                height : 25
              }, {
                layout : 'border',
                region : 'center',
                border : false,
                items : [grid]
              }]
        });

    viewport.doLayout();
  }

  // 删除记录
  function deleteRecord() {
    var result = 'delete';
    Ext.Msg.show({
          title : '再次确认',
          msg : '确定删除信贷合同关系【' + data.contractNo + '】?',
          buttons : Ext.Msg.YESNO,
          fn : function(btn) {
            if (btn == 'yes') {
              Ext.Ajax.request({
                    params : {
                      "id" : cId,
                      "result" : result
                    },
                    method : "POST",
                    url : "contract/delete",
                    callback : function(o, s, r) {
                      var respText = Ext.decode(r.responseText);
                      if (respText.success) {
                        grid.getStore().reload();
                      } else {
                        if (respText.message.length > 0)
                          Ext.Msg.alert('错误', respText.message);

                      }
                    }
                  });
            }
          }
        });
  }
  // 切换过滤查询工具条状态
  function toggleFilterBar() {
    grid.toggleFilterBar();
  }

  // 切换记录编辑状态
  function toggleEditMode() {
    form.loadRecord(new form.dataRecordMeta({
        }));
    win = new Ext.Window({
          title : '编辑信贷关系信息',
          iconCls : 'icon-win',
          autoHeight : true,
          width : 620,
          layout : 'form',
          plain : false,
          buttonAlign : 'right',
          closable : false,
          modal : true,
          items : [form]
        });
    var yesBtn = win.addButton({
          text : '确定'
        }, commitForm, this);
    var cancelBtn = win.addButton({
          text : '关闭'
        }, onWinHide, this);
    win.show();
  }

  // 提交表单，保存新建或修改的信息
  function commitForm() {
    form.commitForm();
    win.hide();
  }

  /* ----------------------- public方法 ----------------------- */
  return {
    /**
     * 初始化
     */
    init : function() {
      createGrid();
      createForm();
      createLayout();
      grid.load();
    }
  }
};